<template>
	<view class="page">
		<view class="top-box" v-if="!groupId">
			<view class="box-class flex">
				<view class="book">
					<image v-if="bookData.imgUrl" :src="bookData.imgUrl" mode=""></image>
					<image v-else src="/static/b1.png" mode=""></image>
				</view>
				<view class="info">
					<view class="row flex justify-between items-center">
						<view class="name">{{bookData.name}}</view>
						<view class="ku flex-shrink-0 pl-20" @click="reset"><image src="/static/reset.png" mode=""></image>重置</view>
					</view>
					<view class="pro">
						<u-line-progress  :percentage="parseInt((bookData.graspTotal*100)/bookData.total)" :height="12" activeColor="#5480F8" inactiveColor="#F2F3F8" :showText="false" ></u-line-progress>
					</view>
					<view class="status d_flex_b">
						<view class="item">
							<text class="label" @click="open1">背词计划</text>
						</view>
						<view class="item">
							<text class="label">已学</text><text style="margin-left: 10rpx;" class="num">{{bookData.graspTotal||0}}</text>
							<!-- <text class="label">总词数  {{bookData.total||0}}</text> -->
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-sticky zIndex="1">
			<view class="nav d_flex_b">
				<view :class="i==curNav?'item ac':'item'" v-for="(item,i) in navList" :key="item.name" @click="changeNav(i)">
					<view class="name">{{item.name}}</view>
					<view class="val">({{item.num}})</view>
				</view>
			</view>
		</u-sticky>
		<view class="list">
			<view class="item d_flex_b" v-for="(item,i) in navList[curNav].data" :key="i">
				<view class="word">{{item.name}}</view>
				<view class="mean">{{item.explain[0].wordSex}}{{item.explain[0].chinExplain}}</view>
			</view>
		</view>
		<view v-if="id == curBook.id" class="page-btn">
			<view class="btn" @click="toStudy">继续背词</view>
		</view>
		<uni-popup ref="popupNumber" type="bottom" :safe-area="false">
			<view class="pop-box">
				<view class="tit">调整每日背词计划</view>
				<view class="picker">
					 <picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange" class="picker-view">
						<picker-view-column>
							<view class="item" v-for="(item,index) in numbers" :key="index">{{item}}个词</view>
						</picker-view-column>
					</picker-view>
				</view>
				<view class="save">
					<view class="btn" @click="saveSet">保存设置</view>
				</view>
			</view>
		</uni-popup>		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navList:[
					{type:0,page:1,data:[],more:true,name:'未学习',num:0},
					{type:2,page:1,data:[],more:true,name:'错词',num:0},
					{type:1,page:1,data:[],more:true,name:'已掌握',num:0},
				],
				curNav:0,
				bookData:'',
				groupId:'',
				curBook:'',
        id: '',
				numbers:[30,50,60,80,100,150,200],
				value:'',
				num:30,
				indicatorStyle: `height: 90rpx;`,				
			};
		},
		onLoad(e) {
			this.bookData=uni.getStorageSync('curBookData');
			
			this.id=e.id;
			this.groupId=e.gid||'';
			this.getList();
			this.getCurBook();
			let dayWordNum=uni.getStorageSync('dayWordNum');
			this.num=dayWordNum||30;
			this.value = [this.numbers.findIndex(v => v == this.num)]
		},
		onReachBottom() {
			if(this.navList[this.curNav].more){
				this.navList[this.curNav].page++;
				this.getList();
			}
		},
		methods:{
			getCurBook(){
				let uid=uni.getStorageSync('userInfo').id;
				this.$http.put('api/client/word/currentBook?userId='+uid,{
					userId:uid
				}).then(res=>{
					if(res.code==200){
						this.curBook=res.data;
					}
				})
			},
			changeNav(i){
				this.curNav=i;
				this.getList();
			},
			getList(){
				let obj={
					bookId:this.id,
					type:this.navList[this.curNav].type,
					current:this.navList[this.curNav].page,
					size:10,
				}
				if(this.groupId){
					obj.groupId=this.groupId;
				}
				this.$http.get1('/api/client/word/getWordsByBookId',obj).then(res=>{
					if(res.code==200){
						this.bookData=res.data;
						this.navList[0].num=this.bookData.notGraspTotal||0;
						this.navList[2].num=this.bookData.graspTotal||0;
						this.navList[1].num=this.bookData.errorTotal||0;
						let arr=res.data.words.map(val=>{
							if(val.wordExplain){
								val.explain=JSON.parse(val.wordExplain)
							}else{
								val.explain=''
							}
							return val;
						})
						if(this.navList[this.curNav].page==1){
							this.navList[this.curNav].data=arr;
						}else{
							this.navList[this.curNav].data.push(...arr);
						}
						if(res.data.words.length<10){
							this.navList[this.curNav].more=false;
						}else{
							this.navList[this.curNav].more=true;
						}
					}
				})
			},
			toStudy(){
				if(this.id!=this.curBook.id){
					uni.showToast({
						title: '非当前在学词书，请先更换词书再继续',
						icon:'none'
					});
				}else{
					uni.navigateTo({
						url:'/pages/remember/remember?gid='+this.groupId+'&id='+this.id+'&isGroup='+this.bookData.isGroup+'&tit='+this.bookData.name
					})
				}
			},
      async confirm() {
        return new Promise(function (resolve, reject) {
          uni.showModal({
            title: `确认重置吗`,
            confirmText: '确认',
            success({confirm, cancel}) {
              if (confirm) {
                resolve()
              }
              if (cancel) {
                reject()
              }
            }
          })
        })
      },
			async reset(){
        await this.confirm()
				this.$http.get1('/api/client/word/clearGraspWordBookData',{
					bookId:this.id
				}).then(res=>{
					if(res.code==200){
						uni.showToast({
							title: '操作成功'
						});
						this.bookData.graspTotal=0;
						this.getList();
					}else{
						uni.showToast({
							title: res.message,
							icon:'none'
						});
					}
				})
			},
			open1(){
				this.$refs.popupNumber.open();
			},
			close1() {
				this.$refs.popupNumber.close();
			},
			bindChange(e){
				this.value=e.detail.value;
			},
			saveSet(){
				this.num=this.numbers[this.value[0]] || 30;
				this.$http.post('/api/client/word/updateWordMyPain',{
					dayWordNum:this.num
				}).then(res=>{
					if(res.code==200){
						uni.setStorageSync('dayWordNum',this.num);
						uni.showToast({
							title: '修改成功'
						});
						this.close1();
					}else{
						uni.showToast({
							title: res.message,
							icon:'none'
						});
					}
				})
			},			
		}
	}
</script>

<style lang="scss">
page{
	background-color: #F2F3F8;
}
.page{
	
}
.top-box{
	background: #fff;
	padding: 20rpx 32rpx;
	box-sizing: border-box;
	.box-class{
		padding:40rpx 30rpx;
		border: 2rpx solid #E4E5EC;
		border-radius: 20rpx;
		box-sizing: border-box;
		.book{
			width: 120rpx;
			height: 148rpx;
			margin-right: 20rpx;
			image{
				width: 120rpx;
				height: 148rpx;
			}
		}
		.info{
			flex: 1;
			.row{
				margin-bottom: 30rpx;
				.name{
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
				}
				.ku{
					font-size: 24rpx;
					color: #BBBFCC;
					image{
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}
				}
			}
			.pro{
				margin-bottom: 20rpx;
			}
			.status{
				
				.item{
					.label{
						font-size: 22rpx;
						color: #BBBFCC;
					}
					.num{
						color: $theme;
						font-size: 22rpx;
						margin: 0 4rpx;
					}
				}
			}
		}
	}
	
}
.nav{
	width: 100%;
	height: 100rpx;
	background-color: #fff;
	.item{
		flex: 1;
		height: 100%;
		text-align: center;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		border-bottom: 4rpx solid #fff;
		color: #575A64;
		.name{
			font-size: 28rpx;
		}
		.val{
			font-size: 20rpx;
			margin-top: 4rpx;
		}
		&.ac{
			border-bottom: 4rpx solid #5480F8;
			color: #5480F8;
		}
	}
}
.list{
	padding: 30rpx 30rpx 200rpx;
	box-sizing: border-box;
	.item{
		width: 100%;
		height: 120rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 2rpx rgba(42,65,152,0.05);
		border-radius: 20rpx ;
		margin-bottom: 20rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		.word{
			width: 300rpx;
			font-size: 28rpx;
			font-weight: bold;
			color: #333333;
		}
		.mean{
			flex: 1;
			width: 350rpx;
			margin-left: 10rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			font-size: 24rpx;
			color: #575A64;
		}
	}
}
.page-btn{
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	background: #FFFFFF;
	box-shadow: inset 0rpx 1rpx 0rpx 2rpx rgba(0,0,0,0.1);
	height: 176rpx;
	padding: 30rpx 62rpx;
	box-sizing: border-box;
	.btn{
		width: 626rpx;
		height: 90rpx;
		background: #5480F8;
		border-radius: 46rpx;
		text-align: center;
		line-height: 90rpx;
		color: #fff;
		font-size: 32rpx;
		font-weight: 500;
	}
}
.pop-box{
	background-color: #fff;
	border-radius: 20rpx 20rpx 0 0;
	.tit{
		height: 120rpx;
		text-align: center;
		line-height: 120rpx;
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}
	.picker-view{
		height: 300rpx;
		.item{
			border:none;
			color: #5480F8;
      background-color: #F5F8FF;
			font-size: 32rpx;
      display: flex;
      justify-content: center;
      align-items: center;
		}
	}
	.save{
		width: 100%;
		height: 124rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		.btn{
			margin: 0 auto;
			width: 626rpx;
			height: 90rpx;
			background: #5480F8;
			border-radius: 46rpx;
			text-align: center;
			line-height: 90rpx;
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
		}
	}
}
</style>
